package guda.house.biz.impl;

import java.util.List;

import guda.house.biz.query.SysVarQuery;
import guda.house.biz.security.AuthUser;
import guda.house.dal.dao.VillageDAO;
import guda.house.dal.dataobject.VillageDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.SysVarBiz;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.SysVarDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class SysVarBizImpl implements SysVarBiz {

    private final static Logger logger = LoggerFactory.getLogger(SysVarBizImpl.class);

    @Autowired
    private SysVarDAO sysVarDAO;
    private VillageDAO villageDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try {
            SysVarDO sysVarDO = sysVarDAO.getSysVarById(id);
            bizResult.data.put("sysVarDO", sysVarDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("query SysVar error", e);
        }
        return bizResult;
    }

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = sysVarDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<SysVarDO> sysVarList = sysVarDAO.selectForPage(baseQuery);
            bizResult.data.put("sysVarList", sysVarList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view SysVar list error", e);
        }
        return bizResult;
    }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            sysVarDAO.delSysVarById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete sysVar error", e);
        }
        return bizResult;
    }

    public BizResult create(SysVarDO sysVarDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = sysVarDAO.insert(sysVarDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create SysVar error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(SysVarDO sysVarDO) {
        BizResult bizResult = new BizResult();
        try {
            sysVarDAO.updateByIdSelective(sysVarDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update SysVar error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult queryForCreate() {
        BizResult bizResult = new BizResult();
        List<Long> villageIdList = AuthUser.getCurrentUser().getVillageIdList();
        if (villageIdList != null || villageIdList.size() == 0) {
            bizResult.success = true;
            return bizResult;
        }
        List<VillageDO> villageDOList = villageDAO.getVillageByIds(villageIdList);
        bizResult.data.put("villageDOList", villageDOList);
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult listForView(Long villageId, String varType) {
        BizResult bizResult = new BizResult();
        SysVarQuery sysVarQuery = new SysVarQuery();
        sysVarQuery.setVarType(varType);
        sysVarQuery.setVillageId(villageId);
        sysVarQuery.setPageSize(100);
        List<SysVarDO> sysVarDOs = sysVarDAO.selectForView(sysVarQuery);
        bizResult.data.put("sysVarList",sysVarDOs);
        bizResult.success = true;
        return bizResult;
    }


}
